﻿#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <assert.h>
#include<stdlib.h>
#include<string.h>

char* convert(char* s, int numRows)
{
    assert(s);
    int len = strlen(s);
    assert(numRows);
    int differ_prev = 2 * numRows - 2;
    int differ_back = 0;
    char* strN = (char*)malloc((len + 1) * sizeof(char));
    assert(strN);
    int i = 0;
    int j = 0;
    for (int count = 0; count < numRows; count++,j = count)
    {
        if (differ_prev && differ_back)
        {
            while (j < len)
            {
                strN[i] = s[j];
                i++;
                j += differ_prev;
                if (j >= len)
                    break;
                strN[i] = s[j];
                i++;
                j += differ_back;
            }
        }
        else if (differ_prev || differ_back)
        {
            while (j < len)
            {
                strN[i] = s[j];
                i++;
                j = j + differ_prev + differ_back;
            }
        }
        else
            return s;
        differ_prev -= 2;
        differ_back += 2;
    }
    strN[len] = '\0';
    return strN;
}

int main()
{
    char* str1 = "abcdefghijk";
    char* str2=convert(str1, 4);
    printf("%s\n", str2);
	return 0;
}